/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.crystalcg.demo;

import htmlparser.*;
import com.crystalcg.filesystem.PureFilePower;
import com.crystalcg.log.Log;
import com.crystalcg.net.HttpLoader;
import com.crystalcg.net.HttpRespons;
import java.nio.charset.Charset;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author lifeng
 */

public class StockDome {
    private ResultSet resultSet;
    private Date date=new Date();
    private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy_MM_dd");
    public StockDome(){
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection=DriverManager.getConnection("jdbc:sqlite:stock.db");
            Statement statement=connection.createStatement();
            resultSet=statement.executeQuery("select * from stock");
            
            next();
        } catch (Exception e) {
            Log.log("数据库读取异常："+e);
            System.out.println(e);
        }
        
    }

    private void next(){
        try {
            if(resultSet.next()){
               HttpLoader loader=new HttpLoader();
               loader.setDefaultContentEncoding(Charset.forName("utf-8").name());
               HttpRespons hr=loader.sendGet("http://222.73.65.88/zjlx/min/"+resultSet.getString("id")+".xml?rt="+Math.random());
               //System.out.println(resultSet.getString("id"));
               Log.log("正在加载："+resultSet.getString("id"));
               PureFilePower filePower=new PureFilePower(System.getProperty("user.dir")+"//data//"+dateFormat.format(date)+"//"+resultSet.getString("id")+".txt");
               filePower.write(hr.getContent());
               next();
            }
        } catch (Exception e) {
            System.out.println(e);
            Log.log("加载异常："+e);
            next();
        }
    }
}
